﻿' frmNetworkProductDetail.vb
' compile with: /doc:frmNetworkProductDetail.xml
' <copyright>Copyright (c) Microsoft Corporation. All rights reserved.
' </copyright>
' <summary>This file contains the product details form for the
' office plan sample.</summary>

Imports System
Imports System.Diagnostics
Imports System.Windows.Forms
Imports System.Data
Imports System.Diagnostics.CodeAnalysis
Imports System.IO


Imports Microsoft.Office.Interop.Visio
Imports Microsoft.Office.Interop.VisOcx
Imports axVisDrawCtrl

Imports NetworkPlanSample
Imports System.Runtime.InteropServices


Partial Class frmNetworkProductDetail

    ''' <summary>The application name is displayed in message box captions.
    ''' </summary>
    Private applicationName As String = ""
    ''' <summary>The Visio application.AlertResponse setting is used to
    ''' suppress error and message dialogs.</summary>
    Private alertResponse As Integer

    Private _shapeControl As AxMicrosoft.Office.Interop.VisOcx.AxDrawingControl
    Public Property shapeControl() As AxMicrosoft.Office.Interop.VisOcx.AxDrawingControl
        Get
            Return Me._shapeControl
        End Get
        Set(ByVal value As AxMicrosoft.Office.Interop.VisOcx.AxDrawingControl)
            Me._shapeControl = value
        End Set
    End Property

    Private _shapeDocument As Microsoft.Office.Interop.Visio.Document
    Public Property shapeDocument() As Microsoft.Office.Interop.Visio.Document
        Get
            Return Me._shapeDocument
        End Get
        Set(ByVal value As Microsoft.Office.Interop.Visio.Document)
            Me._shapeDocument = value
            If shapeControl.Document Is Nothing Then

            End If
        End Set
    End Property

    Private _shapeApplication As Microsoft.Office.Interop.Visio.Application
    Public Property shapeApplication() As Microsoft.Office.Interop.Visio.Application
        Get
            Return Me._shapeApplication
        End Get
        Set(ByVal value As Microsoft.Office.Interop.Visio.Application)
            Me._shapeApplication = value
        End Set
    End Property

    Private _shapeWindow As Microsoft.Office.Interop.Visio.Window
    Public Property shapeWindow() As Microsoft.Office.Interop.Visio.Window
        Get
            Return Me._shapeWindow
        End Get
        Set(ByVal value As Microsoft.Office.Interop.Visio.Window)
            Me._shapeWindow = value
        End Set
    End Property


    Private _shapePage As Microsoft.Office.Interop.Visio.Page
    Public Property shapePage() As Microsoft.Office.Interop.Visio.Page
        Get
            Return Me._shapePage
        End Get
        Set(ByVal value As Microsoft.Office.Interop.Visio.Page)
            Me._shapePage = value
        End Set
    End Property


    ''' <summary>This frmNetworkProductDetail custom constructor requires the
    ''' product information and metafile image as input. This
    ''' allows the details of the specified product to be displayed.
    ''' </summary>
    ''' <param name="productInformation">The information on all products.
    ''' </param>
    ''' <param name="productId">The ID of the product to display on the
    ''' form.</param>
    ''' <param name="productShape">The metafile containing a picture of the
    ''' product.</param>
    Public Sub New _
                (ByVal productInformation As ProductData, _
                 ByVal productId As Integer, _
                 ByVal productShape As Microsoft.Office.Interop.Visio.Document)

        Me.New(productInformation, productId)


        Try



        Catch ex As Exception

            Dim errWin As ErrorWindow = New ErrorWindow("construct", ex)
            errWin.ShowDialog()

        End Try
    End Sub

    ''' <summary>This frmNetworkProductDetail custom constructor requires the
    ''' product information as input. This gets the details of the specified
    ''' product and fills the controls with the detail values.</summary>
    ''' <param name="productInformation">The information on all products.
    ''' </param>
    ''' <param name="productId">The ID of the product to display on the
    ''' form.</param>
    <SuppressMessage("Microsoft.Globalization", "CA1303:DoNotPassLiteralsAsLocalizedParameters")> _
    Public Sub New _
            (ByVal productInformation As ProductData, _
             ByVal productId As Integer)

        Me.New()

        Dim productRow As DataRow
        Dim dataFilter As String = Nothing

        ' Only display the other details if the product ID is valid.
        If productId = ProductData.InvalidProductId Then

            ' Show the message on the form telling the user that the
            ' product ID isn't valid.
            Me.noProductsLabel.Visibility = System.Windows.Visibility.Visible

            ' Do not display the product ID.
            productIDValue.Text = ""
        Else

            If productInformation Is Nothing Then
                Throw New ArgumentNullException("productInformation")
            End If

            ' Display the product ID.
            productIDValue.Text = productId.ToString(System.Globalization.CultureInfo.CurrentCulture)

            Dim productRows As DataRow() = Nothing

            ' Get the row in the product data table that contains the
            ' product with the specified product ID.
            If productInformation.ProductDataTable IsNot Nothing Then
                dataFilter = (ProductData.ProductIdColumnName & " = ") + productId.ToString(System.Globalization.CultureInfo.CurrentCulture)
                productRows = productInformation.ProductDataTable.[Select](dataFilter)
            End If

            If productRows.Length < 1 Then

                Dim msgWin As ErrorWindow = New ErrorWindow("Selection not in Product Data Table", "Selection not in DataGrid")
                msgWin.ShowDialog()
                Exit Sub

            End If

            If productRows.GetUpperBound(0) >= 0 Then
                productRow = productRows(0)

                ' Put the detail values into the controls on the form.
                rangeNameValue.Text = productRow(ProductData.RangeColumnName).ToString()
                descriptionValue.Text = productRow(ProductData.DescriptionColumnName).ToString()
                typeValue.Text = productRow(ProductData.TypeDescriptionColumnName).ToString()
                costValue.Text = [String].Format(System.Globalization.CultureInfo.CurrentCulture, "{0:c}", productRow(ProductData.CostPriceColumnName))
                wholesaleValue.Text = [String].Format(System.Globalization.CultureInfo.CurrentCulture, "{0:c}", productRow(ProductData.WholesalePriceColumnName))
                retailValue.Text = [String].Format(System.Globalization.CultureInfo.CurrentCulture, "{0:c}", productRow(ProductData.RetailPriceColumnName))
                laborHoursValue.Text = productRow(ProductData.LaborHoursColumnName).ToString()
            End If
        End If
    End Sub

    ''' <summary>The frmNetworkProductDetail default constructor initializes the
    ''' components of the form.
    ''' </summary>
    Public Sub New()

        ' Initializing the components of the form is required for
        ' Windows Form Designer support.
        InitializeComponent()

        ' Assume that there is a valid product and hide the
        ' no-products label.
        noProductsLabel.Visibility = System.Windows.Visibility.Hidden

        'get the wrapper for the visio control
        Dim axVisLib As axVisDraw = wfh.Child
        If shapeControl Is Nothing Then
            shapeControl = axVisLib.AxDrawingControl
        End If

        setUpVisioDrawing()

    End Sub


    Private Sub okButton_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs)
        shapeApplication = Nothing
        shapeDocument = Nothing
        shapeWindow = Nothing
        shapePage = Nothing
        Me.Close()
    End Sub

    ''' <summary>The setUpVisioDrawing method initializes the Visio drawing
    ''' that is displayed in the drawing control.</summary>
    ''' <remarks>This method throws a COMException on error.</remarks>
    Private Sub setUpVisioDrawing()

        Dim pageLeft As Double
        Dim pageTop As Double
        Dim pageWidth As Double
        Dim pageHeight As Double


        Try


            ' Cache the application name for use in message box captions.
            applicationName = shapeControl.Window.Application.Name

            ' Cache the AlertResponse setting in a member variable.
            alertResponse = shapeControl.Window.Application.AlertResponse

            ' Hide all built-in docked windows (shape search, 
            ' custom properties, etc.).
            For i As Integer = shapeControl.Window.Windows.Count To 1 Step -1
                Dim visWindow As Microsoft.Office.Interop.Visio.Window
                Dim windowType As Integer

                visWindow = shapeControl.Window.Windows.ItemEx(i)
                windowType = visWindow.Type

                If windowType = CInt(VisWinTypes.visAnchorBarBuiltIn) Then

                    Select Case visWindow.ID
                        Case CInt(VisWinTypes.visWinIDCustProp), CInt(VisWinTypes.visWinIDDrawingExplorer), CInt(VisWinTypes.visWinIDMasterExplorer), CInt(VisWinTypes.visWinIDPanZoom), CInt(VisWinTypes.visWinIDShapeSearch), CInt(VisWinTypes.visWinIDSizePos), _
                        CInt(VisWinTypes.visWinIDStencilExplorer)

                            visWindow.Visible = False
                            Exit Select
                        Case Else

                            Exit Select
                    End Select
                End If
            Next

            ' Use the Visio window to set the visible user
            ' interface parts of the window.
            Dim targetWindow As Microsoft.Office.Interop.Visio.Window
            targetWindow = DirectCast(shapeControl.Window, Microsoft.Office.Interop.Visio.Window)

            targetWindow.ShowRulers = 0
            targetWindow.ShowPageTabs = False
            targetWindow.ShowScrollBars = 0
            targetWindow.ShowGrid = 0
            targetWindow.Zoom = 1.0R

            ' Position the furniture shapes relative to the page.
            targetWindow.GetViewRect(pageLeft, pageTop, pageWidth, pageHeight)

            'initialize pretty much everything
            shapeApplication = targetWindow.Application
            If shapeWindow Is Nothing Then
                shapeWindow = shapeControl.Window
            Else
                shapeWindow = Nothing
                shapeWindow = shapeControl.Window
            End If

            If shapeDocument Is Nothing Then
                shapeDocument = shapeControl.Document
            Else
                shapeDocument = Nothing
                shapeDocument = shapeControl.Document
            End If

            If shapePage Is Nothing Then
                shapePage = shapeWindow.Page
            Else
                shapePage = Nothing
                shapePage = shapeWindow.Page
            End If


            ' The drawing control is displaying a newly opened document.
            ' Set the saved flag to true.
            shapeControl.Document.Saved = True

        Catch [error] As COMException

            ' Display the error.
            Utility.DisplayException(Strings.ComErrorMessage, [error], alertResponse)
            Throw

        Catch ex As Exception

            Dim errWin As ErrorWindow = New ErrorWindow("setupdetailvisio", ex)
            errWin.ShowDialog()

        End Try




    End Sub





End Class

